﻿using System.Collections.Generic;
using System.Data;

namespace ConfExpert.Forms.ImportWizards.Stok
{
    public partial class Tan_Tekstil : XITopCekiListesi
    {
        int YuklemeId;
        public Tan_Tekstil(int YuklemeId):
            base(YuklemeId)
        {
            InitializeComponent();

            this.YuklemeId = YuklemeId;
            Init("Tan Tekstil Çeki Listesi", true);
            this.FileDone += this_FileDone;
            //this.SplitterWidthPercentage = 30;
        }

        void this_FileDone(DataSet ds, string FileName)
        {
            DataTable dt = ds.Tables[0];
            DataRowCollection rows = dt.Rows;
            DataColumnCollection cols = dt.Columns;
            
            int basliksatiri = 0;
            for (int i = 0; i < dt.Rows.Count; i++)
                if (dt.Rows[i][0].ToString() == "Barkod")
                {
                    basliksatiri = i;
                    break;
                }

            Dictionary<string, int> indexes = new Dictionary<string, int>();
            for (int colIndex = 0; colIndex < dt.Columns.Count; colIndex++)
            {
                DataRow dr0 = dt.Rows[basliksatiri];
                switch (dr0[colIndex].ToString())
                {
                    case "Barkod": indexes.Add("VendorRollId", colIndex); break;
                    case "Kontrol mt.": indexes.Add("Qty", colIndex); break;
                    case "Top Kalitesi": indexes.Add("Kalite2", colIndex); break;
                }
            }


            for (int i = basliksatiri + 1; i < dt.Rows.Count; i++)
            {
                DataRow dr = rows[i];
                string article = rows[3][8].ToString();

                if (Article.Select("Article='" + article + "'").Length == 0)
                {
                    DataRow drArticle = Article.NewRow();
                    drArticle["Article"] = article;
                    Article.Rows.Add(drArticle);
                }
                DataRow drRoll = Roll.NewRow();

                if (dr[indexes["VendorRollId"]].ToString() != "Toplam")
                    if (dr[indexes["Qty"]].ToString() != "")
                    {
                        drRoll["Article"] = article;
                        drRoll["VendorRollId"] = dr[indexes["VendorRollId"]];
                        drRoll["Qty"] = decimal.Parse(dr[indexes["Qty"]].ToString().Replace(" mt", ""), System.Globalization.CultureInfo.InvariantCulture.NumberFormat);
                        drRoll["Kalite2"] = dr[indexes["Kalite2"]];

                        Roll.Rows.Add(drRoll);
                    }
            }

        }

    }
}